/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.managedbean;

import au.edu.swin.swinvehicle.vehicle.VehicleDetailedTO;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerException;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerRemote;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeManagerException;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeManagerRemote;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeTO;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerException;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerRemote;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeTO;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Administrator
 */
public class VehicleJSFManagedBean {

    public static final String LIST_VEHICLES = "listVehicles";
    public static final String ADD_VEHICLE = "addVehicle";
    public static final String EDIT_VEHICLE = "editVehicle";
    @EJB
    private VehicleManagerRemote vehicleManagerBean;
    @EJB
    private VehicleTypeManagerRemote vehicleTypeManagerBean;
    @EJB
    private VehicleLicenceTypeManagerRemote vehicleLicenceTypeManagerBean;
    private VehicleDetailedTO vehicle;

    /** Creates a new instance of VehicleJSFManagedBean */
    public VehicleJSFManagedBean() {
        this.vehicle = new VehicleDetailedTO();
        this.vehicle.setVehicleType(new VehicleTypeTO());
        this.vehicle.setVehicleLicenceType(new VehicleLicenceTypeTO());
    }

    public VehicleDetailedTO getVehicle() {
        return vehicle;
    }

    public void setVehicle(VehicleDetailedTO vehicle) {
        this.vehicle = vehicle;
    }

    public String addVehicle() {
        try {
            this.vehicleManagerBean.addVehicle(this.vehicle);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Added:");
            msg.setDetail("The new vehicle has been successfully added");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleSuccessNotification", msg);

            return LIST_VEHICLES;
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot add new vehicle. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleErrorNotification", msg);

            return ADD_VEHICLE;
        }
    }

    public List<VehicleDetailedTO> getVehicles() {
        List<VehicleDetailedTO> vehicles = new ArrayList<VehicleDetailedTO>();
        try {
            vehicles = this.vehicleManagerBean.getAllDetailedVehicles();
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return vehicles;
    }

    public List<SelectItem> getVehicleTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle type ---"));
        try {
            List<VehicleTypeTO> vehicleTypes = this.vehicleTypeManagerBean.getAllVehicleTypes();
            for (VehicleTypeTO vehicleType : vehicleTypes) {
                selectItems.add(new SelectItem(vehicleType.getId(), vehicleType.getCode()));
            }
        } catch (VehicleTypeManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public List<SelectItem> getVehicleLicenceTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle licence type ---"));
        try {
            List<VehicleLicenceTypeTO> vehicleLicenceTypes = this.vehicleLicenceTypeManagerBean.getAllVehicleLicenceTypes();
            for (VehicleLicenceTypeTO vehicleLicenceType : vehicleLicenceTypes) {
                selectItems.add(new SelectItem(vehicleLicenceType.getId(), vehicleLicenceType.getCode()));
            }
        } catch (VehicleLicenceTypeManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public String editVehicle() {
        String vehicleIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleId");
        try {
            Long vehicleId = Long.parseLong(vehicleIdString);
            this.vehicle = this.vehicleManagerBean.getVehicle(vehicleId);
            if (this.vehicle == null) {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid vehicle ID:");
                msg.setDetail("The vehicle does not exist. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleErrorNotification", msg);

                return LIST_VEHICLES;
            } else {
                return EDIT_VEHICLE;
            }
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit vehicle. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleErrorNotification", msg);

            return LIST_VEHICLES;
        }
    }

    public String saveVehicle() {
        try {
            this.vehicleManagerBean.editVehicle(this.vehicle);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Updated:");
            msg.setDetail("The vehicle has been successfully updated");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleSuccessNotification", msg);

            return LIST_VEHICLES;
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit vehicle. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleErrorNotification", msg);

            return EDIT_VEHICLE;
        }
    }

    public String deleteVehicle() {
        String vehicleIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleId");
        try {
            Long vehicleId = Long.parseLong(vehicleIdString);
            this.vehicleManagerBean.deleteVehicle(vehicleId);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Deleted:");
            msg.setDetail("The vehicle has been successfully deleted");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleSuccessNotification", msg);
            
            return LIST_VEHICLES;
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot delete vehicle. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleErrorNotification", msg);

            return LIST_VEHICLES;
        }
    }
}
